package com.she.iiicoursecenter.activity.tab4_message;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.she.iiicoursecenter.Splash;
import com.she.iiicoursecenter.iiiApplication;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_MainActivity;
import com.she.iiicoursecenter.activity.template.TemplateActivity;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.GetItemListBean;
import com.she.iiicoursecenter.event.connect.bean.GetMsgBean;
import com.she.iiicoursecenter.event.connect.event.GetMsgEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.tab4.Tab4_MainView;

public class Tab4_MainActivity extends TemplateActivity {
	//
	//
	private Tab4_MainView	mMainView;
	private ProgressDialog dialog ;
	
	public Tab4_MainActivity() {
		// TODO Auto-generated constructor stub
	}
	
	private Handler notifyHandler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			// TODO Auto-generated method stub
			if (msg.obj == null) {
				mMainView.notifyAdapter();
				dialog.cancel();
			}else{
				mMainView.notifyAdapter();
				Toast.makeText(Tab4_MainActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				dialog.cancel();
			}

			super.dispatchMessage(msg);
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		mMainView = new Tab4_MainView(this);
		
		dialog = new ProgressDialog(this);
		dialog.setMessage("讀取中...");
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		dialog.show();
		
		//
		setView(mMainView);
		setContentView(mMainView);		
		
		//訊息快遞
		GetMsgEvent getMsgEvent = new GetMsgEvent(null);
		getMsgEvent.addCallback(new IEventCallback() {
			
			public void result(Event event, Object result) {
				if ( result instanceof List<?>) {
					((iiiApplication) Tab4_MainActivity.this.getApplication()).setMsgData((List<GetMsgBean>) result);
					List<GetMsgBean> data = ((iiiApplication) Tab4_MainActivity.this.getApplication()).getMsgData();
					for (int i = 0; i < data.size(); i++) {
						int msgid = ((iiiApplication) Tab4_MainActivity.this.getApplication()).getMsgData().get(i).getMsgId();
						long msgTime = ((iiiApplication) Tab4_MainActivity.this.getApplication()).getMsgData().get(i).getTime();
						if (KenEventManager.getInstance().getMsgIdRead(msgid) < msgTime) {
							data.get(i).setIsNew(true);
						}
					}
					notifyHandler.sendEmptyMessage(0);
				}else{
					
					((iiiApplication) Tab4_MainActivity.this.getApplication()).setMsgData(new ArrayList<GetMsgBean>());
					Message msg = notifyHandler.obtainMessage(0, "網路異常，請重新操作。");
					notifyHandler.sendMessage(msg);
				}
			}
			public void progress(Event event, int progress, String tmpPath) {
			}
		});
		KenEventManager.getInstance().execute(getMsgEvent);
		//
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onAttachedToWindow() {
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	}

	@Override
	public void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		super.onDetachedFromWindow();
	}

}
